package com.itheima.mall.api.sms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.exception.BusinessException;
import com.itheima.mall.mapper.sms.HomeRecommendProductMapper;
import com.itheima.mall.pojo.sms.HomeNewProduct;
import com.itheima.mall.pojo.sms.HomeRecommendProduct;
import com.itheima.mall.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@DubboService
public class HomeRecommendProductServiceApiImpl implements HomeRecommendProductServiceApi{
    @Autowired
    private HomeRecommendProductMapper homeRecommendProductMapper;
    @Override
    public void recommendProductCreate(HomeRecommendProduct homeRecommendProduct) {
        homeRecommendProductMapper.insert(homeRecommendProduct);
    }

    @Override
    public Boolean RecommendProductDelete(Long[] ids) {
        Boolean flag = true;
        List<HomeRecommendProduct> homeRecommendProductList = homeRecommendProductMapper.selectBatchIds(Arrays.asList(ids));
        for (HomeRecommendProduct homeRecommendProduct : homeRecommendProductList) {
            if(homeRecommendProduct.getRecommendStatus() == 1){
                flag = false;
            }else {
                homeRecommendProductMapper.deleteById(homeRecommendProduct.getId());
            }
        }
        return flag;
    }

    @Override
    public PageResult<HomeRecommendProduct> recommendProductList(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        Page pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<HomeRecommendProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(recommendStatus!=null,HomeRecommendProduct::getRecommendStatus,recommendStatus);
        queryWrapper.like(StrUtil.isNotEmpty(productName),HomeRecommendProduct::getProductName,productName);
        queryWrapper.orderByDesc(HomeRecommendProduct::getSort);
        Page page = homeRecommendProductMapper.selectPage(pageInfo, queryWrapper);
        PageResult pageResult = new PageResult<>();
        pageResult.setTotal(Convert.toInt(page.getTotal()));
        pageResult.setPageSize(Convert.toInt(page.getSize()));
        pageResult.setPageNum(Convert.toInt(page.getCurrent()));
        pageResult.setTotalPage(Convert.toInt(page.getPages()));
        pageResult.setList(page.getRecords());
        return pageResult;
    }

    @Override
    public List<HomeRecommendProduct> findRecommendProductByIds(Long[] ids) {
        return homeRecommendProductMapper.selectBatchIds(Arrays.asList(ids));
    }

    @Override
    public void update(HomeRecommendProduct homeRecommendProduct) {
        homeRecommendProductMapper.updateById(homeRecommendProduct);
    }

    @Override
    public HomeRecommendProduct findRecommendProductById(Long id) {
        return homeRecommendProductMapper.selectById(id);
    }
}